import { NextApiRequest, NextApiResponse } from "next";
import { initORM } from "../../../utils/orm";
import { User } from "../../../entities/User";
import { getCurrentUser, getUser } from "@/services";

async function getUserInfo(req: NextApiRequest, res: NextApiResponse<APIRes>) {
    const em = await initORM();

    const user = req.query.userid
        ? await getUser(req)
        : await getCurrentUser(req);

    if (!user) {
        res.status(404).json({
            success: false,
            data: {},
            message: "User not found",
        });
        return;
    }

    res.status(200).json({
        success: true,
        data: user,
        message: "Get user info success",
    });
}

export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse<APIRes>
) {
    if (req.method === "GET") {
        getUserInfo(req, res);
    } else {
        res.status(405).json({
            success: false,
            data: {},
            message: "Method not allowed",
        });
    }
}

// curl -X POST -H "Content-Type: application/json" -d '{"name": "John Doe"}' http://localhost:3000/api/author
// curl http://localhost:3000/api/author
// curl -X DELETE -H "Content-Type: application/json" -d '{"id": 1}' http://localhost:3000/api/author
